package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.entity.CurriculumResultStatus;
import edu.zqu.instructionalMatters.project.mapper.CurriculumResultStatusMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表执行状态字典 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表执行状态字典")
public class CurriculumResultStatusController {
    @Autowired
    private CurriculumResultStatusMapper curriculumResultStatusMapper;

    @ApiOperation(value = "获取课表执行状态字典列表")
    @RequestMapping(value = "/curriculumResultStatuses", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false)
    })
    public ZQUResult getList(String name,
                             String englishName
    ) {
        CurriculumResultStatus curriculumResultStatus = new CurriculumResultStatus()                             //合成对象
                .setName(name).setEnglishName(englishName);
        List<CurriculumResultStatus> list = curriculumResultStatusMapper.findAll(curriculumResultStatus);        //查询出list
        return ZQUResult.success(list);                                                                          //返回list
    }

    @ApiOperation(value = "获取课表执行状态字典信息", response = CurriculumResultStatus.class)
    @GetMapping(value = "/curriculumResultStatus/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CurriculumResultStatus curriculumResultStatus = curriculumResultStatusMapper.selectById(id);            //查出数据对象
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumResultStatus);                      //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/curriculumResultStatus", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(String name,
                         String englishName,
                         BigInteger byWho) {
        CurriculumResultStatus curriculumResultStatus = new CurriculumResultStatus();               //初始化
        curriculumResultStatus.setName(name);                                                       //设置中文名称
        curriculumResultStatus.setEnglishName(englishName);                                         //设置英文名称
        curriculumResultStatus.setByWho(byWho);                                                     //设置操作者
        curriculumResultStatusMapper.insert(curriculumResultStatus);                                //执行插入
        return ZQUResult.success();                                                                 //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/curriculumResultStatus/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(@PathVariable BigInteger id,
                            String name,
                            String englishName,
                            BigInteger byWho) {
        CurriculumResultStatus curriculumResultStatus = curriculumResultStatusMapper.selectById(id);//先查出旧记录
        curriculumResultStatus.setByWho(byWho);                                                     //设置操作者
        curriculumResultStatusMapper.updateById(curriculumResultStatus);                            //更新操作者
        curriculumResultStatusMapper.deleteById(id);                                                //逻辑删除
        curriculumResultStatus = new CurriculumResultStatus();                                      //初始化
        curriculumResultStatus.setName(name);                                                       //设置中文名称
        curriculumResultStatus.setEnglishName(englishName);                                         //设置英文名称
        curriculumResultStatus.setByWho(byWho);                                                     //设置操作者
        curriculumResultStatus.setLastVersion(id);                                                  //设置上一版本
        curriculumResultStatusMapper.insert(curriculumResultStatus);                                //执行插入
        return ZQUResult.success();                                                                 //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/curriculumResultStatus/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CurriculumResultStatus curriculumResultStatus = curriculumResultStatusMapper.selectById(id);//先查出旧记录
        curriculumResultStatus.setByWho(byWho);                                                     //设置操作者
        curriculumResultStatusMapper.updateById(curriculumResultStatus);                            //更新操作者
        curriculumResultStatusMapper.deleteById(id);                                                //逻辑删除
        return ZQUResult.success();                                                                 //操作成功返回
    }
}

